<?php
/**
 * Created by PhpStorm.
 * Name: App忘记密码
 * User: 董坤鸿
 * Date: 2016/12/5
 * Time: 10:52
 */

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use App\Services\Repo\UserRepo;
use Illuminate\Http\Request;
use App\Plugins\Sms\SendInfo;
use Illuminate\Support\Facades\Redis;

class ForgetPasswordController extends Controller
{

    /**
     * 发送验证码
     * @param Request $request
     * @param UserRepo $userRepo
     * @return array
     */
    public function getVerify(Request $request, UserRepo $userRepo)
    {
        $mobile = $request->input('mobile');
        if (!preg_match('#^13[\d]{9}$|^14[5,7]{1}\d{8}$|^15[^4]{1}\d{8}$|^17[0,6,7,8]{1}\d{8}$|^18[\d]{9}$#', $mobile)) {
            return $this->_error('您输入的手机号格式错误');
        }
        $_filter = ['mobile' => $mobile];
        $user = $userRepo->getUser($_filter);
        if (empty($user)) {
            return $this->_error("您输入的帐号不存在！");
        }
        $verify = rand(100000, 999999);   //随机数
        if(Redis::exists($mobile)){
            $res = Redis::zrange($mobile, 0, -1);
            Redis::zrem($mobile, $res[0]);
            //缓存验证码
            Redis::zadd($mobile, 0, $verify);
            Redis::expire($mobile, config('app.customizes.expiration'));
        }else{
            //缓存验证码
            Redis::zadd($mobile, 0, $verify);
            Redis::expire($mobile, config('app.customizes.expiration'));
        }

        //发送短信
        $sendInfo = new SendInfo();
        $code = [
            'sendNum' => $verify,
            'usefulTime' => config('app.customizes.expiration') / 60,
        ];
        $sendInfo->Send(1, $code, $mobile, null, null);
    }

    /**
     * 忘记密码修改
     * @param Request $request
     * @param UserRepo $userRepo
     * @return array|string
     */
    public function forgotPassword(Request $request, UserRepo $userRepo)
    {
        $data = $request->all();
        if(empty(Redis::exists($data['mobile']))){
            return $this->_error('您的验证码已失效，请重新获取！');
        }
        $res = Redis::zrange($data['mobile'], 0, -1);   //取出验证码
        if ($data['verify'] != $res[0]) {
            $score = Redis::zscore($data['mobile'], $res[0]);   //获取次数
            //判断验证码是否输入三次错误
            if ($score >= 3) {
                Redis::zrem($data['mobile'], $res[0]);
                return $this->_error('您的验证码已失效，请重新获取！');
            }
            Redis::zincrby($data['mobile'], 1, $res[0]);    //错误的时候次数加1
            return $this->_error('验证码有误，请重新填写！');
        }

        //验证验证码
        if (empty($data['password'])) {
            return "验证码正确";
        }

        $_filter = ['mobile' => $data['mobile']];
        $user = $userRepo->getUser($_filter);
        if (empty($user)) {
            return $this->_error("您输入的帐号不存在！");
        }
        $updateData['password'] = bcrypt($data['password']);
        $_updateFilter['id'] = $user->id;
        $isSave = $userRepo->update($_updateFilter, $updateData);
        if($isSave){
            Redis::zrem($data['mobile'], $res[0]);
            return "重置新密码成功!";
        }else{
            return $this->_error('重置新密码失败!');
        }
    }
}